package com.zhl.media.app.mvp.presenter;

import android.app.Application;
import android.widget.Toast;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.muddzdev.styleabletoast.StyleableToast;
import com.zhl.media.app.R;
import com.zhl.media.app.greendao.AdJustDao;
import com.zhl.media.app.greendao.ElevatorDao;
import com.zhl.media.app.greendao.MediaPositionDao;
import com.zhl.media.app.greendao.ProjDao;
import com.zhl.media.app.model.AdJust;
import com.zhl.media.app.model.Elevator;
import com.zhl.media.app.model.MediaPosition;
import com.zhl.media.app.mvp.contract.AdjustedContract;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonsdk.data.UserUtil;
import com.zhl.media.commonsdk.entity.AdJustMedia;
import com.zhl.media.commonsdk.utils.EmptyUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;


@ActivityScope
public class AdjustedPresenter extends BasePresenter<AdjustedContract.Model, AdjustedContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public AdjustedPresenter(AdjustedContract.Model model, AdjustedContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void selectAdjust(){
        ProjDao projDao = EntityManager.getInstance(mApplication).getProjDao();
        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        ElevatorDao elevatorDao = EntityManager.getInstance(mApplication).getElevatorDao();
        MediaPositionDao mediaPositionDao = EntityManager.getInstance(mApplication).getMediaPositionDao();
        List<AdJust> adJusts = adJustDao.queryBuilder().where(AdJustDao.Properties.AdjustStatus.eq(1),AdJustDao.Properties.IsAdjust.eq(0)).list();
        List<AdJustMedia> list = new ArrayList<>();
        for (int i = 0; i < adJusts.size(); i++) {
            MediaPosition oldMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJusts.get(i).getOldMediaId())).unique();
            AdJustMedia adJustMedia = new AdJustMedia();
            adJustMedia.setProjectName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldMedia.getProjectId())).unique().getName());
            adJustMedia.setBuildingName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldMedia.getBuildingId())).unique().getName());
            adJustMedia.setUnitName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldMedia.getUnitId())).unique().getName());
            Elevator elevator = elevatorDao.queryBuilder().where(ElevatorDao.Properties.Id.eq(oldMedia.getElevatorId())).unique();
            adJustMedia.setElevatorName(elevator.getName());
            adJustMedia.setInstallPosition(oldMedia.getInstallPosition());
            adJustMedia.setSchemeName(oldMedia.getSchemeName());
            adJustMedia.setAlphabetIdentification(oldMedia.getAlphabetIdentification());
            adJustMedia.setElevatorPosition(elevator.getElevatorPosition());
            adJustMedia.setSchemeId(oldMedia.getSchemeId());
            adJustMedia.setId(adJusts.get(i).getId());
            list.add(adJustMedia);
        }
        mRootView.onSuccess(list);
    }

    public void updateAdjust(String adjustId,String newMediaId){
        UserUtil.setAdjust(true);
        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        MediaPositionDao mediaPositionDao = EntityManager.getInstance(mApplication).getMediaPositionDao();
        AdJust adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.Id.eq(adjustId)).unique();
        // 旧媒体位
        MediaPosition oldMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJust.getOldMediaId())).unique();
        // 新媒体位
        MediaPosition newMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(newMediaId)).unique();

        if(EmptyUtils.isNotEmpty(newMedia.getStartTime())){
            if(oldMedia.getEndTime()<newMedia.getStartTime()){
                adJust.setNewMediaId(newMediaId);
                adJust.setIsAdjust(1);
                adJustDao.update(adJust);
                Map map = new HashMap();
                map.put("mediaId",newMedia.getId());
                map.put("unitId",newMedia.getUnitId());
                map.put("buildingId",newMedia.getBuildingId());
                map.put("projectId",newMedia.getProjectId());
                map.put("oldMediaId",oldMedia.getId());
                map.put("oldUnitId",oldMedia.getUnitId());
                map.put("oldBuildingId",oldMedia.getBuildingId());
                map.put("oldProjectId",oldMedia.getProjectId());
                mRootView.onAdjust(map);
                StyleableToast.makeText(mApplication, "调整成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
            }else {
                StyleableToast.makeText(mApplication, "调整失败，此媒体位已被其它方案占用", Toast.LENGTH_SHORT, R.style.mytoast).show();
            }
        }else {
            adJust.setNewMediaId(newMediaId);
            adJust.setIsAdjust(1);
            adJustDao.update(adJust);
            Map map = new HashMap();
            map.put("mediaId",newMedia.getId());
            map.put("unitId",newMedia.getUnitId());
            map.put("buildingId",newMedia.getBuildingId());
            map.put("projectId",newMedia.getProjectId());
            map.put("oldMediaId",oldMedia.getId());
            map.put("oldUnitId",oldMedia.getUnitId());
            map.put("oldBuildingId",oldMedia.getBuildingId());
            map.put("oldProjectId",oldMedia.getProjectId());
            mRootView.onAdjust(map);
            StyleableToast.makeText(mApplication, "调整成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        }

    }
}
